home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / COP.ZIP / SHAPE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-21  |  7.0 KB  |  331 lines

  1. #include <stdarg.h>
  2. #include "shape.h"
  3.  
  4.  
  5. /*  Shape  */
  6.  
  7. vFt_def(Shape,Shape)  =  {
  8.     vF_value(SH,SH,show),
  9.     vF_value(SH,SH,destruct)
  10. };
  11.  
  12. /* ARGSUSED */
  13. #pragma argsused
  14. vf_def(void,SH,SH,show,(Shape * thiS, int xxpose,
  15.     int yxpose, unsigned scale))
  16.     {}
  17.  
  18. vf_def(void,SH,SH,destruct,(Shape * thiS, 
  19.     unsigned nobj, int malloced))
  20. {
  21.     struct_destruct(SH,thiS,nobj,malloced);
  22. }
  23.  
  24.  
  25. struct_initVFTs_def(SH,(Shape * thiS,
  26.     void * descendanT_0, vFT_0_decl(Shape)))
  27. {
  28.     poly_assign(thiS,descendanT_0);
  29.     vFT_assign(Shape,Shape,
  30.         thiS,vFT_0_name(Shape));
  31. }
  32.  
  33. struct_init_def(Shape,SH,_,
  34.     (Shape * thiS_0, unsigned nobj,
  35.     unsigned x, unsigned y))
  36. {
  37.   unsigned i;
  38.  
  39.   if (!nobj)
  40.     return (Shape *)0;
  41.   if (!thiS_0) if ((thiS_0 = (Shape *)
  42.     SH_malloc(nobj))
  43.     == (Shape *)0)
  44.     return (Shape *)0;
  45.   for (i = 0; i < nobj; i++)  {
  46.     struct_initVFTs(SH,(&thiS_0[i],(void *)0,
  47.     vFT0(Shape)));
  48.     thiS_0[i].x = x;
  49.     thiS_0[i].y = y;
  50.  
  51.   }  /* for */
  52.   return thiS_0;
  53. }
  54.  
  55. struct_destruct_def(Shape,SH)
  56. {
  57.   unsigned i;
  58.  
  59.   if (!thiS_0 || !nobj)
  60.     return;
  61.  
  62.   for (i = nobj; i--; /* no reinit */)  {
  63.     struct_initVFTs(SH,(&thiS_0[i],
  64.       (void *)0,vFT0(Shape)));
  65.   }
  66.  
  67.   if (malloced)
  68.     SH_free(thiS_0);
  69. }
  70.  
  71.  
  72.  
  73. /*  Segment  */
  74.  
  75. polyBaseThiS_0_def(SG,Shape,Segment)
  76.  
  77. vFt_def(Segment,Shape)  =  {
  78.     vF_value(SG,SH,show),
  79.     vF_value(SG,SH,destruct)
  80. };
  81.  
  82. vf_def(void,SG,SH,show,(Shape * thiS, int xxpose,
  83.     int yxpose, unsigned scale))
  84. {
  85.     unsigned i;
  86.     Segment * SGthiS = SH_SegmentThiS(thiS);
  87.  
  88.     if (SGthiS->shapeCount)
  89.        for (i = 0; i < SGthiS->shapeCount; i++)
  90. /*lint -e506 */
  91.           if(SGthiS->shapes[i])
  92.          SH_show(SGthiS->shapes[i],
  93.             (int)SG_getx(SGthiS)+xxpose,
  94.             (int)SG_gety(SGthiS)+yxpose,
  95.             scale);
  96. /*lint +e506 */
  97. }
  98.  
  99. vf_def(void,SG,SH,destruct,(Shape * thiS,
  100.     unsigned nobj, int malloced))
  101. {
  102.     struct_destruct(SG,SH_SegmentThiS(thiS),
  103.         nobj,malloced);
  104. }
  105.  
  106. struct_initVFTs_def(SG,(Segment * thiS,
  107.     void * descendanT_0
  108.     , vFT_0_decl(Shape)))
  109. {
  110.     poly_assign(thiS,descendanT_0);
  111.     struct_initVFTs(SH,
  112.         (SG_ShapeThiS(thiS), (void *) thiS,
  113.         vFT_0_name(Shape)));
  114. }
  115.  
  116. struct_init_def(Segment,SG,_,
  117.     (Segment * thiS_0, unsigned nobj,
  118.         unsigned x, unsigned y,
  119.         unsigned shapeCount, ...))
  120. {
  121.   unsigned i, j, fail;
  122.   int malloced = 0;
  123.   va_list shapes;
  124. /*
  125.     Since all shapes must be dynamically allocated
  126.     nobj is allowed only to be 1. nobj could be
  127.     greater than 1 if shapes had a virtual clone
  128.     function or better yet a link count.  To do so
  129.     would have made the example even more
  130.     complicated.
  131. */
  132.  
  133.  
  134.   if (!nobj || (nobj > 1) || !shapeCount)
  135.     return (Segment *)0;
  136.   if (!thiS_0) if ((thiS_0 = (Segment *)
  137.     SG_malloc(nobj))
  138.     == (Segment *)0)
  139.     return (Segment *)0;
  140.   else
  141.     malloced = 1;
  142.   for (i = fail = 0; i < nobj; i++)  {
  143.     if (!struct_init(SH,_,(SG_ShapeThiS(&thiS_0[i]),
  144.       1,x,y)))  {
  145.       fail = 1;
  146.       break;
  147.     }
  148.     struct_initVFTs(SG,(&thiS_0[i], (void *)0,
  149.       vFT_value(Segment,Shape)));
  150.  
  151.     thiS_0[i].shapes = (Shape **) 0;
  152.     thiS_0[i].shapeCount = 0;
  153.     if ((thiS_0[i].shapes = (Shape **)MALLOC(sizeof(Shape *)
  154.        *shapeCount)) != (Shape **)0)  {
  155.        va_start(shapes,shapeCount);
  156.        for (j = 0; j < shapeCount; j++)
  157.      thiS_0[i].shapes[j] = va_arg(shapes,Shape *);
  158.        va_end(shapes);
  159.        thiS_0[i].shapeCount = shapeCount;
  160.     }
  161.     else {
  162.       va_start(shapes,shapeCount);
  163.       for (j = 0; j < shapeCount; j++)
  164.     SH_delete(va_arg(shapes,Shape *),1);
  165.       va_end(shapes);
  166.       struct_destruct(SH,SG_ShapeThiS(&thiS_0[i]),1,0);
  167.       fail = 1;
  168.       break;
  169.     }
  170.  
  171.   }  /* for */
  172.  
  173.   if (fail)  {
  174.     /*
  175.     All indices >= "i" have already been destructed
  176.     as required.  Indices i-1 to 0 are now
  177.     destructed via the destructor since we're
  178.     inside the constructor of the instance level if
  179.     i > 0.  Remember that if this is an
  180.     intermediate constructor call that i == 0
  181.     and malloced == 0 always so no further
  182.     processing is required at this level and NULL
  183.     is returned to the calling level constructor.
  184.     */
  185.     if (i)
  186.       struct_destruct(SG,thiS_0,i,malloced);
  187.     else if (malloced)
  188.       SG_free(thiS_0);
  189.     return (Segment *) 0;
  190.   }
  191.  
  192.   return thiS_0;
  193. }
  194.  
  195. struct_destruct_def(Segment,SG)
  196. {
  197.   unsigned i, j;
  198.  
  199.   if (!thiS_0 || !nobj)
  200.     return;
  201.  
  202.   for (i = nobj; i--; /* no reinit */)  {
  203.  
  204.     struct_initVFTs(SG,(&thiS_0[i], (void *)0,
  205.       vFT_value(Segment,Shape)));
  206.  
  207.     if (thiS_0[i].shapeCount)  {
  208.       for (j = 0; j < thiS_0[i].shapeCount; j++)
  209.      SH_delete(thiS_0[i].shapes[j],1);
  210.       FREE(thiS_0[i].shapes);
  211.     }
  212.  
  213.     struct_destruct(SH,SG_ShapeThiS(&thiS_0[i]),1,0);
  214.  
  215.   }
  216.  
  217.   if (malloced)
  218.     SG_free(thiS_0);
  219. }
  220.  
  221.  
  222.  
  223. /*  Circle  */
  224.  
  225. polyBaseThiS_0_def(CR,Shape,Circle)
  226.  
  227. vFt_def(Circle,Shape)  =  {
  228.     vF_value(CR,SH,show),
  229.     vF_value(CR,SH,destruct)
  230. };
  231.  
  232. vf_def(void,CR,SH,show,(Shape * thiS, int xxpose,
  233.     int yxpose, unsigned scale))
  234. {
  235.     Circle * CRthiS = SH_CircleThiS(thiS);
  236.  
  237. /*lint -e506 */
  238.     circle((int)CR_getx(CRthiS)+xxpose,
  239.         (int)CR_gety(CRthiS)+yxpose,
  240.         (int)(CRthiS->radius*scale));
  241. /*lint +e506 */
  242. }
  243.  
  244. vf_def(void,CR,SH,destruct,(Shape * thiS,
  245.     unsigned nobj, int malloced))
  246. {
  247.     struct_destruct(CR,SH_CircleThiS(thiS),
  248.         nobj,malloced);
  249. }
  250.  
  251. struct_initVFTs_def(CR,(Circle * thiS,
  252.     void * descendanT_0
  253.     , vFT_0_decl(Shape)))
  254. {
  255.     poly_assign(thiS,descendanT_0);
  256.     struct_initVFTs(SH,
  257.         (CR_ShapeThiS(thiS), (void *) thiS,
  258.         vFT_0_name(Shape)));
  259. }
  260.  
  261. struct_init_def(Circle,CR,_,
  262.     (Circle * thiS_0, unsigned nobj,
  263.         unsigned radius,
  264.         unsigned x, unsigned y))
  265. {
  266.   unsigned i, fail;
  267.   int malloced = 0;
  268.  
  269.   if (!nobj)
  270.     return (Circle *)0;
  271.   if (!thiS_0) if ((thiS_0 = (Circle *)
  272.     CR_malloc(nobj))
  273.     == (Circle *)0)
  274.     return (Circle *)0;
  275.   else
  276.     malloced = 1;
  277.   for (i = fail = 0; i < nobj; i++)  {
  278.     if (!struct_init(SH,_,(CR_ShapeThiS(&thiS_0[i]),
  279.       1,(x?x:radius),(y?y:radius))))  {
  280.       fail = 1;
  281.       break;
  282.     }
  283.     struct_initVFTs(CR,(&thiS_0[i], (void *)0,
  284.       vFT_value(Circle,Shape)));
  285.  
  286.     thiS_0[i].radius = radius;
  287.  
  288.   }  /* for */
  289.  
  290.   if (fail)  {
  291.     /*
  292.     All indices >= "i" have already been destructed
  293.     as required.  Indices i-1 to 0 are now
  294.     destructed via the destructor since we're
  295.     inside the constructor of the instance level if
  296.     i > 0.  Remember that if this is an
  297.     intermediate constructor call that i == 0
  298.     and malloced == 0 always so no further
  299.     processing is required at this level and NULL
  300.     is returned to the calling level constructor.
  301.     */
  302.     if (i)
  303.       struct_destruct(CR,thiS_0,i,malloced);
  304.     else if (malloced)
  305.       CR_free(thiS_0);
  306.     return (Circle *) 0;
  307.   }
  308.  
  309.   return thiS_0;
  310. }
  311.  
  312. struct_destruct_def(Circle,CR)
  313. {
  314.   unsigned i;
  315.  
  316.   if (!thiS_0 || !nobj)
  317.     return;
  318.  
  319.   for (i = nobj; i--; /* no reinit */)  {
  320.  
  321.     struct_initVFTs(CR,(&thiS_0[i], (void *)0,
  322.       vFT_value(Circle,Shape)));
  323.  
  324.     struct_destruct(SH,CR_ShapeThiS(&thiS_0[i]),1,0);
  325.  
  326.   }
  327.  
  328.   if (malloced)
  329.     CR_free(thiS_0);
  330. }
  331.